package appapi.service.impl;

import appapi.Global.BusinessException;
import appapi.Request.RoleReq;
import appapi.Request.record.ScenePageReq;
import appapi.Request.record.SceneReq;
import appapi.Request.record.SectReq;
import appapi.Request.record.SectionPageReq;
import appapi.Vo.SceneDeviceVo;
import appapi.Vo.ScenePageVo;
import appapi.Vo.brider.SceneDetailVo;
import appapi.Vo.brider.SceneDevicePageVo;
import appapi.Vo.brider.SceneVo;
import appapi.dao.FaceRecordMapper;
import appapi.dao.SceneDeviceMapper;
import appapi.dao.SceneMapper;
import appapi.dto.bridge.AiExceptionDto;
import appapi.dto.bridge.SceneCheckDto;
import appapi.entity.Zhongjiao.SceneDto;
import appapi.entity.Zhongjiao.SceneEquipmentDto;
import appapi.entity.auth.RoleEntity;
import appapi.entity.basic.UserInfo;
import appapi.entity.bridge.FaceRecordEntity;
import appapi.entity.bridge.SceneDeviceEntity;
import appapi.entity.bridge.SceneEntity;
import appapi.service.mango.FaceRecordService;
import appapi.service.mango.SceneService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author dhq
 * @version 1.0
 * @date 2023/9/11  12:26
 */

@Service
public class SceneServiceImpl extends ServiceImpl<SceneMapper, SceneEntity> implements SceneService {

    @Autowired
    SceneMapper sceneMapper;
    @Autowired
    SceneDeviceMapper sceneDeviceMapper;

    @Override
    public int deleteByPrimaryKey(Long sceneId) {
        return sceneMapper.deleteByPrimaryKey(sceneId);
    }

    @Override
    public int insert(SceneEntity scene) {
        return sceneMapper.insert(scene);
    }

    @Override
    public int insertSelective(SceneEntity scene) {
        return sceneMapper.insertSelective(scene);
    }

    @Override
    public SceneEntity selectByPrimaryKey(Long sceneId) {
        return sceneMapper.selectByPrimaryKey(sceneId);
    }

    @Override
    public int updateByPrimaryKeySelective(SceneEntity scene) {
        return sceneMapper.updateByPrimaryKeySelective(scene);
    }

    @Override
    public int updateByPrimaryKey(SceneEntity scene) {
        return sceneMapper.updateByPrimaryKey(scene);
    }

    @Override
    public int insertScene(UserInfo userInfo, SceneVo dto) {
        //判断这个编号是否已经存在，sceneNo唯一
        String sceneNo = dto.getSceneNo();
        int iRet = sceneMapper.countSceneNo(sceneNo);
        //iRet>0,场景编号已存在不能重复添加
        if (iRet > 0) {
            return -1;
        }
        SceneEntity entity = dto.Entity();
        entity.setLastModifyTime(LocalDateTime.now());
        entity.setCreateTime(LocalDateTime.now());
        entity.setSceneStatus(0);
        entity.setCreateUser(userInfo.getUserid());
        entity.setSceneStart(LocalDateTime.now());
        entity.setSceneCount(dto.getList().size());
        //添加场景
        int i = sceneMapper.insertSelective(entity);
        if (i > 0) {
            //获取场景secId
            Long sceneId = entity.getSceneId();
            //添加场景设备
            List<appapi.Vo.brider.SceneDeviceVo> list = dto.getList();
            if (list.size() > 0) {
                for (appapi.Vo.brider.SceneDeviceVo deviceVo : list) {
                    SceneDeviceEntity deviceEntity = deviceVo.entity();
                    deviceEntity.setSceneId(sceneId);
                    deviceEntity.setProjId(dto.getProjId());
                    deviceEntity.setSectId(deviceVo.getSectId());
                    deviceEntity.setCreateTime(LocalDateTime.now());
                    deviceEntity.setLastModifyTime(LocalDateTime.now());
                    deviceEntity.setCreateUser(userInfo.getUserid());
                    deviceEntity.setLastModifyUser(userInfo.getUserid());
                    sceneDeviceMapper.insertSelective(deviceEntity);
                }
            }
        }
        return i;
    }

    /**
     * 编辑场景信息
     *
     * @param dto
     * @return
     */
    @Override
    public int UpdateScene(UserInfo userInfo, SceneVo dto) {
        SceneEntity entity = dto.Entity();
        entity.setLastModifyTime(LocalDateTime.now());
        entity.setLastModifyUser(userInfo.getUserid());
        entity.setSceneCount(dto.getList().size());
        entity.setSceneCount(dto.getList().size());
        //更新场景
        int i = sceneMapper.updateByPrimaryKeySelective(entity);
        if (i > 0) {
            //删除场景已有设备
            sceneDeviceMapper.deleteBySceneId(dto.getSceneId());
            List<appapi.Vo.brider.SceneDeviceVo> list = dto.getList();
            if (list.size() > 0) {
                //更新该场景下的设备id
                for (appapi.Vo.brider.SceneDeviceVo deviceVo : list) {
                    SceneDeviceEntity sceneDeviceEntity = deviceVo.entity();
                    //设备绑定场景
                    sceneDeviceEntity.setSceneId(dto.getSceneId());
                    sceneDeviceEntity.setProjId(dto.getProjId());
                    sceneDeviceEntity.setSectId(deviceVo.getSectId());
                    sceneDeviceEntity.setCreateTime(LocalDateTime.now());
                    sceneDeviceEntity.setLastModifyTime(LocalDateTime.now());
                    sceneDeviceEntity.setCreateUser(userInfo.getUserid());
                    sceneDeviceEntity.setLastModifyUser(userInfo.getUserid());
                    sceneDeviceMapper.insert(sceneDeviceEntity);
                }
            }
        }
        return i;
    }

    /**
     * 获取场景详情
     *
     * @param sceneId
     * @return
     */
    @Override
    public SceneDeviceVo getSceneDeviceDetail(Long sceneId) {
        SceneDeviceVo sceneDeviceVo = new SceneDeviceVo();
        SceneEntity sceneEntity = sceneMapper.selectByPrimaryKey(sceneId);
        //查询绑定的设备
        List<SceneDeviceEntity> lists = sceneDeviceMapper.selectSenceDeviceEntity(sceneId);
        sceneDeviceVo.setSceneEntity(sceneEntity);
        sceneDeviceVo.setList(lists);
        return sceneDeviceVo;
    }


    /**
     * 场景列表
     *
     * @param sceneReq
     * @return
     */
    @Override
    public List<ScenePageVo> getSecList(SceneReq sceneReq) {
        List<ScenePageVo> secList = sceneMapper.getSecList(sceneReq);
        return secList;
    }

    /**
     * 场景分页查询
     *
     * @param req
     * @return
     */
    @Override
    public PageInfo<ScenePageVo> GetSceneListByPage(ScenePageReq req) {
        String json = JSON.toJSONString(req);
        SceneReq dataReq = JSON.parseObject(json, new TypeReference<SceneReq>() {
        });
        PageInfo<ScenePageVo> pageInfo = PageHelper.startPage(req.getCurrPage(), req.getPageSize())
                .doSelectPageInfo(() -> this.getSecList(dataReq));

        return pageInfo;
    }

    @Override
    public List<ScenePageVo> GetSceneList(SceneReq req) {
        return sceneMapper.getSecList(req);
    }

    /**
     * @param sceneId
     * @return
     */
    @Override
    public SceneDetailVo getSceneDetail(Long sceneId) {
        SceneDetailVo sceneDetail = sceneMapper.getSceneDetail(sceneId);
        if (sceneDetail == null) {
            throw new BusinessException(500, "场景详情查询失败");
        }
        return sceneDetail;
    }

    /**
     * 删除场景
     *
     * @param secId
     * @return
     */
    @Override
    public Integer deleteSceneById(Long secId) {
        int iRet = sceneMapper.count(secId);
        if (iRet > 0) {
            return -1;
        } else {
            iRet = sceneMapper.deleteByPrimaryKey(secId);
        }
        return iRet;
    }

    /**
     * 获取场景设备列表
     *
     * @param sceneId
     * @return
     */
    @Override
    public List<SceneDeviceEntity> getSceneDataDeviceList(Long sceneId) {
        return sceneDeviceMapper.selectDataDeviceBySecId(sceneId);
    }

    @Override
    public List<SceneCheckDto> getSceneListByOrgaId(Long orgaId)
    {
        return sceneMapper.selectSceneCheckListByOrgaId(orgaId);
    }
}
